<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
//use App\Models\logfile;

class UseLogController extends Controller
{
    protected $name = '操作日志';
    protected $find = ['id', 'Ip', 'operator', 'editdate', 'type', 'billcaption', 'des'];
    /**
     * 查询
     */
    public function find(Request $request)
    {
        try {
            $param = json_decode($request->param, true);

            $operator = $this->I($param['operator'] ?? null);
            $datebegin = $this->I($param['datebegin'] ?? null);
            $dateend = $this->I($param['dateend'] ?? null);
            $billcaption = $this->I($param['billcaption'] ?? null);
            $type = $this->I($param['type'] ?? null);
            $page = intval($this->I($param['page'] ?? 1));
            $pagecount = intval($this->I($param['pagecount'] ?? 50));
            $where = ' a.isdel=0 ';

            if (!empty($operator)) {
                $where .= " and a.operator = '$operator' ";
            }
            if (!empty($datebegin)) {
                $where .= " and a.editdate >= '$datebegin' ";
            }
            if (!empty($dateend)) {
                $where .= " and a.editdate < '$dateend 23:59:59' ";
            }
            if (!empty($billcaption)) {
                $where .= " and a.billcaption = '$billcaption' ";
            }
            if (!empty($type)) {
                $where .= " and a.type = '$type' ";
            }
            $temp = DB::table('UseLog as a')
                ->select($this->find)
                ->whereRaw($where)
                ->orderBy('a.editdate', 'desc')
                ->paginate($pagecount, ['id'], 'page', $page)
                ->toArray(); // 控制页码，默认每页 10 条
            $res = $temp['data'];
            $allcount = $temp['total'];
            return $this->ajaxReturn(['code' => 1, 'msg' => $this->name . '|查询成功!', 'count' => $allcount, 'data' => $res]);
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|查询失败!','error'=>$e->getMessage()]);
        }
    }
}
